//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: test.c 757 2007-05-10 08:19:31Z roger $
//

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SP_TABLE_MAX_LEN 27

typedef struct _IllTable {
         char *str1;
         char *str2;
} IllTable;

typedef struct _SPCell {
	        char keyname;
	        char *sheng;
	        char *yun1;
	        char *yun2;
} SPCell;

typedef SPCell SPTable[SP_TABLE_MAX_LEN];

int flag=0;

static const IllTable illTable[11] = {
	{"wang", "huang"},
	{"an", "ang"},
	{"en", "eng"},
	{"in", "ing"},
	{"zh", "z"},
	{"ch", "c"},
	{"sh", "s"},
	{"n", "l"},
	{"l", "r"},
	{"f", "h"},
	{"f", "hu"}
};
static const SPTable sp_table = {
	{'a',	NULL,	"a",	NULL},
	{'b',	"b",	"ou",	NULL},
	{'c',	"c",	"iao",	NULL},
	{'d',	"d",	"uang",	"iang"},
	{'e',	NULL,	"e",	NULL},
	{'f',	"f",	"en",	NULL},
	{'g',	"g",	"eng",	NULL},
	{'h',	"h",	"ang",	NULL},
	{'i',	"ch",	"i",	NULL},
	{'j',	"j",	"an",	NULL},
	{'k',	"k",	"ao",	NULL},
	{'l',	"l",	"ai",	NULL},
	{'m',	"m",	"ian",	NULL},
	{'n',	"n",	"in",	NULL},
	{'o',	".",	"o",	"uo"},
	{'p',	"p",	"un",	NULL},
	{'q',	"q",	"iu",	NULL},
	{'r',	"r",	"uan",	"er"},
	{'s',	"s",	"ong",	"iong"},
	{'t',	"t",	"ue",	NULL},
	{'u',	"sh",	"u",	NULL},
	{'v',	"zh",	"ui",	"NULL"},
	{'w',	"w",	"ia",	"ua"},
	{'x',	"x",	"ie",	NULL},
	{'y',	"y",	"uai",	"v"},
	{'z',	"z",	"ei",	NULL},
	{';',	NULL,	"ing",	NULL}
};

	char* yunmuy[]={"uai","un","ei","ia",NULL};

static const char * sy_table[26][27] = {
{ "a",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","ei","en","eng","i","ian","iao","ie","in","ing","o","u",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","e","en","eng","i","ong","ou","u","uan","ui","un","uo",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","e","ei","en","eng","i","ia","ian","iao","ie","ing","iu","ong","ou","u","uan","ui","un","uo",NULL,NULL,NULL,NULL, },
{ "e",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","an","ang","ei","en","eng","o","ou","u",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","e","ei","en","eng","ong","ou","u","ua","uai","uan","uang","ui","un","uo",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","e","ei","en","eng","m","ng","ong","ou","u","ua","uai","uan","uang","ui","un","uo",NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","e","en","eng","i","ong","ou","u","ua","uai","uan","uang","ui","un","uo",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "i","ia","ian","iang","iao","ie","in","ing","iong","iu","u","uan","ue","un",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","e","ei","en","eng","ong","ou","u","ua","uai","uan","uang","ui","un","uo",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","e","ei","eng","i","ia","ian","iang","iao","ie","in","ing","iu","o","ong","ou","u","v","uan","ve","un","uo",NULL, },
{ "" ,"a","ai","an","ang","ao","e","ei","en","eng","i","ian","iao","ie","in","ing","iu","o","ou","u",NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "" ,"a","ai","an","ang","ao","e","ei","en","eng","g","i","ia","ian","iang","iao","ie","in","ing","iu","ong","ou","u","v","uan","ve","uo", },
{ "a","ai","an","ang","ao","e","ei","en","eng","er","o","ou",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, }, 
{ "a","ai","an","ang","ao","ei","en","eng","i","ian","iao","ie","in","ing","o","ou","u",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "i","ia","ian","iang","iao","ie","in","ing","iong","iu","u","uan","ue","un",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "an","ang","ao","e","en","eng","i","ong","ou","u","uan","ui","un","uo",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","e","en","eng","i","ong","ou","u","uan","ui","un","uo",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","e","eng","i","ian","iao","ie","ing","ong","ou","u","uan","ui","un","uo",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","e","ei","en","eng","i","ou","u","ua","uai","uan","uang","ui","un","uo",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","e","ei","en","eng","i","ong","ou","u","ua","uai","uan","uang","ui","un","uo",NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ei","en","eng","o","u",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "i","ia","ian","iang","iao","ie","in","ing","iong","iu","u","uan","ue","un",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","an","ang","ao","e","i","in","ing","o","ong","ou","u","uan","ue","un",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
{ "a","ai","an","ang","ao","e","ei","en","eng","i","ong","ou","u","uan","ui","un","uo",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, },
};

int syflag=0;
char *providelist=NULL;
char shengmu;
char yunmu;
char shengmua[]={"a"};
char shengmue[]={"e"};

int get_relation_list(char key)
{
				char *findaddr=NULL;
				char *str1;
				char *str2;
				int spkeynum;
				int j;
					if(isalpha(key))
									spkeynum=key - 'a';
					else if (key==';')
									spkeynum=26;
					else
					{
									printf("invaliaed input ,please input alpha num or ';'.\n");
									exit (1);
					}


				if (shengmu=='a')
				{
								providelist=shengmua;
								return (1);
				}

				if (shengmu=='e')
				{
								providelist=shengmue;
								return (1);
				}

				if (shengmu==';')
				{
								printf("the sheng mu cant be ';'.\n");
								return (0);
				}

					
								if(syflag==0)
								{
														char *str1=(char*)malloc(strlen(sp_table[spkeynum].sheng)+1);
														memset(str1,0,strlen(str1)+1);
														strcpy(str1,sp_table[spkeynum].sheng);
														providelist=str1;
														syflag=1;
								}
								else
								{
														for(j=0;j<27;j++)
														{
															if(strcmp(sy_table[shengmu - 'a'][j],sp_table[spkeynum].yun1)==0)
															{
																	char *str1=(char*)malloc(strlen(sp_table[spkeynum].yun1)+1);
																	memset(str1,0,strlen(str1)+1);
																	strcpy(str1,sp_table[spkeynum].yun1);
																	providelist=str1;
																	syflag=0;
																	return (1);
															}
														
															if (sp_table[spkeynum].yun2 != NULL)
															{
																	if(strcmp(sy_table[shengmu - 'a'][j],sp_table[spkeynum].yun2)==0)
																	{
																		char *str2=(char*)malloc(strlen(sp_table[spkeynum].yun2)+1);
																		memset(str2,0,strlen(str2)+1);
																		strcpy(str2,sp_table[spkeynum].yun2);
																		providelist=str2;
																		syflag=0;
																		return (2);
																	}
															}
														}
														syflag=0;
														return 3;
								}
}

int
main(int argc,char **argv)
{
				int i;
				char *p;
				printf("Please input a sheng mu:\n");
				shengmu=getchar();
				getchar();
				get_relation_list(shengmu);
				if((shengmu=='a') || (shengmu=='e'))
				{
								printf("The yun mu is :%c.\n",shengmu);
								exit (1);
				}
				else
				printf("The sheng mu is :%s\n",providelist);
				printf("Please input a yun mu:\n");
				yunmu=getchar();
				get_relation_list(yunmu);
				printf("The yun mu is :%s\n",providelist);
}


// vi:ts=4:nowrap:cin:expandtab
